// pkg_shop/pages/shopSet/shopSet.js
import request from '../../../utils/http.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dianPuId: 0, //店铺id
    mingCheng:'',
    logoImg:'',
    teSe:'',
    diZhi:'',
    dianHua:'',
    imageUrl1:'/image/cameraImg.png',
    imageUrl2:'/image/cameraImg.png',
    imageUrl3:'/image/cameraImg.png',
    imageUrl4:'/image/cameraImg.png',
    imageUrl5:'/image/cameraImg.png',
    imgIndex:0,//5:logo,6-10:imageUrl1-5
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        dianPuId: wx.getStorageSync(app.globalData.SHOPID), //店铺id
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  this.getShoppingInfo();
  },

  
  //获取店铺信息
  getShoppingInfo: function () {

    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    var that = this
   // console.log("that.data.dianPuId111:"+that.data.dianPuId)
    let data = {
      dianPuId: that.data.dianPuId
    }
    request.get('dianPu/basicInfo', data, res => {

     //console.log("店铺基本信息:" + JSON.stringify(res.data))
      that.setData({
        mingCheng: res.data.mingCheng,
        logoImg: res.data.logo,
        teSe: res.data.teSe,
        diZhi: res.data.diZhi,
        dianHua: res.data.dianHua,
      })

      res.data.beiJingTuList.forEach((item, index) => {
          if(index==0){
            that.setData({
              imageUrl1:item==''?'/image/cameraImg.png':item,

            })
          }
          if(index==1){
            that.setData({
              imageUrl2:item==''?'/image/cameraImg.png':item,

            })
          }
          if(index==2){
            that.setData({
              imageUrl3:item==''?'/image/cameraImg.png':item,
            })
          }
          if(index==3){
            that.setData({
              imageUrl4:item==''?'/image/cameraImg.png':item,
            })
          }
          if(index==4){
            that.setData({
              imageUrl5:item==''?'/image/cameraImg.png':item,
            })
          }
      })
      wx.hideLoading()

    }, err => {

    })

  },

    //店铺设置
  toSetItem: function (e) {
    var index = e.currentTarget.dataset.index;
    //console.log(index)
      wx.navigateTo({
        url: '/pkg_shop/pages/shopEdit/shopEdit?index='+index,
      })
    },


     //上传图片
  bindKaotiImgFront: function (e) {
    let imgIndex = e.currentTarget.dataset.index //当前索引
    var that = this
    that.setData({
      imgIndex:imgIndex
    })
    wx.showActionSheet({
      itemList: ['拍照', '从相册选择'],
      success: function (res) {
        //拍照
        if (res.tapIndex == 0) {
          that.openCamera()
        } else if (res.tapIndex == 1) {
          //从相册选择
          that.openPhoto()
        }
      }
    })
    // }
  },

  /**
   * 打开照相机
   */
  openCamera: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["camera"],
      success: function (res) {
        var filePath = res.tempFilePaths[0]
        var sizeImg=res.tempFiles[0].size

        if(that.data.imgIndex==5&&sizeImg>512000){//logo大于500KB
          wx.showToast({
            title: '店铺Logo图片不能大于500K', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1500, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })

        }else if(that.data.imgIndex>5&&sizeImg>1024000){//背景图
          wx.showToast({
            title: '店铺背景图不能超过1M', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1500, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })
        }else{
          that.uploadToQiniu(filePath)
        }
        // console.log('sizeImg:'+sizeImg)
      
      },
    })
  },

  /**
   * 从相册选择
   */
  openPhoto: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album"],
      success: function (res) {
        var filePath = res.tempFilePaths[0]
        var sizeImg=res.tempFiles[0].size

        if(that.data.imgIndex==5&&sizeImg>512000){//logo大于500KB
          wx.showToast({
            title: '店铺Logo图片不能大于500K', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1500, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })

        }else if(that.data.imgIndex>5&&sizeImg>1024000){//背景图
          wx.showToast({
            title: '店铺背景图不能超过1M', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1500, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })
        }else{
          that.uploadToQiniu(filePath)
        }
      },
    })
  },



  /**
   * 上传图片到七牛
   */
  uploadToQiniu: function (filePath) {
    wx.showLoading({
      title: '上传中...',
      mask: true
    })
    var that = this
    console.log(filePath)
    wx.request({
      url: app.globalData.base_url + 'resource/getQiNiuToken',
      success: function (res) {

        // console.log("res.data.uptoken:" + res.data.uptoken),
        // console.log("res.data.key:" + res.data.key)
        wx.uploadFile({
          url: 'https://up-z1.qbox.me', //七牛云存储华东地区
          filePath: filePath,
          name: 'file',
          formData: {
            token: res.data.uptoken,
            key: res.data.key
          },
          success: function (res) {
          //  console.log("imageUrl:res.data:" + res.data)
            var dataObject = JSON.parse(res.data);
            var imageUrl = app.globalData.base_qiniu_url + dataObject.key;
              // console.log("imageUrl:" + imageUrl)
            app.globalData.shopIsChange='true';
            if(that.data.imgIndex==5){
              that.setData({
                logoImg: imageUrl
              })

              wx.setStorageSync(app.globalData.ShopLogo, imageUrl);
            }

            if(that.data.imgIndex==6){
              that.setData({
                imageUrl1: imageUrl
              })
            }

            if(that.data.imgIndex==7){
              that.setData({
                imageUrl2: imageUrl
              })
            }

            if(that.data.imgIndex==8){
              that.setData({
                imageUrl3: imageUrl
              })
            }
            if(that.data.imgIndex==9){
              that.setData({
                imageUrl4: imageUrl
              })
            }
            if(that.data.imgIndex==10){
              that.setData({
                imageUrl5: imageUrl
              })
            }

            that.updataShopInfo(that.data.imgIndex,imageUrl)

          }


      
        })
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },


  //更新店铺信息
  updataShopInfo:function(index,img){
    var that = this

   console.log("index："+index)
   console.log("img："+img)
    let data = {
      dianPuId: that.data.dianPuId,
      leiXing:index,
      ziDuan:img
    }
    request.get('dianPu/setDianPuMingZi', data, res => {
     
      if(res.data.status==1){
        app.globalData.shopIsChange='true'

      }else{
        wx.showToast({
          title: '修改失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      }
    }, err => {
      wx.showToast({
        title: '修改失败，请联系技术人员', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  },

  //删除背景图
  delImg:function(e){
    let imgIndex = e.currentTarget.dataset.index //当前索引
    var that=this

    if(imgIndex==6){
      that.setData({
        imageUrl1: '/image/cameraImg.png'
      })
    }

    if(imgIndex==7){
      that.setData({
        imageUrl2: '/image/cameraImg.png'
      })
    }

    if(imgIndex==8){
      that.setData({
        imageUrl3: '/image/cameraImg.png'
      })
    }
    if(imgIndex==9){
      that.setData({
        imageUrl4: '/image/cameraImg.png'
      })
    }
    if(imgIndex==10){
      that.setData({
        imageUrl5: '/image/cameraImg.png'
      })
    }
    that.updataShopInfo(imgIndex,'')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})